<?php

class Database
{
    private $conn;
    private $result;
    private $prep;
    public function Database($host, $username, $password, $dbname)
    {
        $this->conn = new mysqli($host, $username, $password, $dbname);
        if ($this->conn->connect_errno) {
            die("数据库链接失败：" . $this->conn->connect_error);
        }
        $this->conn->select_db($dbname) or die("选择数据库失败：" . $this->conn->error);
        $this->conn->set_charset("utf8") or die("设置字符集失败：" . $this->conn->error);
    }

    public function query($sql)
    {
        if ($this->result) {
            $this->result = NULL;
        }
        $this->result = $this->conn->query($sql);
        if (! $this->result) {
            printf("Error: %s\n", $this->conn->error);
            exit();
        }
        return true;
    }

    public function getResult()
    {
        return $this->result;
    }

    public function fetch_assoc()
    {
        return $this->result->fetch_assoc();
    }

    public function isEmpty()
    {
        return $this->result->num_rows == 0;
    }

    public function getRows()
    {
        return $this->result->num_rows;
    }
    public function fetch_all(){
        return $this->result->fetch_all(MYSQLI_ASSOC);
    }
}
?>